package com.ishanggang.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class GeoHashUtil {
    @Autowired
    private RedisTemplate redisTemplate;

    public Long redisGeoAdd(String key, double longitude, double latitude, String name) {
        Long addedNum = this.redisTemplate.opsForGeo().add(key, new Point(longitude, latitude), name);
        return addedNum;
    }

    public GeoResults<RedisGeoCommands.GeoLocation<String>> redisNearByXY(String key, double longitude, double latitude, Integer distance, Integer count) {
        Circle circle = new Circle(new Point(longitude, latitude), new Distance(distance.intValue(), Metrics.KILOMETERS));
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(count.intValue());
        GeoResults<RedisGeoCommands.GeoLocation<String>> results = this.redisTemplate.opsForGeo().radius(key, circle, args);
        return results;
    }
}
